﻿using Facebook;
using FriendAnalyzer.Infrastructure.Facebook.DataAccess;
using FriendAnalyzer.Infrastructure.Facebook.Interfaces;
using FriendAnalyzer.Infrastructure.Facebook.Model;
using FriendAnalyzer.Infrastructure.Facebook.Repositories;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FriendAnalyzer.Infrastructure.Facebook
{
    public class FriendsAPI : IFriendsAPI
    {
        private IFriendRepository _repository;
        private UserRepository _userRepo;

        public FriendsAPI(IFriendRepository repository)
        {
            _repository = repository;
        }

        public async Task<List<FacebookUser>> AggregateFriendsDataAsync()
        {
            var friends = await _repository.GetFacebookFriendsAsync();
            var events = await _repository.GetAllFriendEventsAsync();
            var likes = await _repository.GetAllPageLikesAsync();
            var groups = await _repository.GetAllGroupsAsync();
            var checkins = await _repository.GetAllCheckinsAsync();

            foreach (var friend in friends)
            {
                var userId = friend.UserID;
                friend.Likes = likes.Where(l => l.LikedBy == userId)
                                    .ToList();
                friend.Events = events.Where(e => e.AttendedBy == userId)
                                      .ToList();
                friend.Groups = groups.Where(e => e.MemberID == userId)
                                      .ToList();
                friend.CheckIns = checkins.Where(e => e.UserID == userId)
                                          .ToList();
            }

            return friends;
        }
    }
}